package com.shenhao.hpfa.util;

import java.util.List;

/**
 * JSON片段重组演示程序
 */
public class JsonFragmentDemo {
    
    public static void main(String[] args) {
        System.out.println("=== JSON片段重组演示 ===");
        
        // 测试1: 基本的两个片段重组
        testBasicReassembly();
        
        // 测试2: 一个报文包含多个JSON
        testMultipleJsonInOneMessage();
        
        // 测试3: 一个报文包含一个半JSON
        testOneAndHalfJson();
    }
    
    private static void testBasicReassembly() {
        System.out.println("\n--- 测试1: 基本的两个片段重组 ---");
        
        String fragment1 = "{\"SysTime\":\"2023-05-12 19:16:45\",\"bid\":202,\"mid\":\"de3f228f-4ca2-4574-ab63-9df513915ce3\",\"DeviceSN\":\"JE18300197\",\"ip\":\"192.168.1.100\",\"Children\":[{\"ClientID\":\"JE18300197\",\"Voltage\":[0,0,0,0],\"Current\":[0,0,0,0],\"Power\":[0,0,0,0],\"Energy\":[0,0,0,0],\"Temperature\":[5876,0,0,0],\"ElectricStatus\":0,\"SwitchStatus\":0,";
        String fragment2 = "\"Mode\":0}]}";
        
        String channelId = "test-basic";
        JsonFragmentReassembler.clearBuffer(channelId);
        
        System.out.println("片段1长度: " + fragment1.length());
        System.out.println("片段2长度: " + fragment2.length());
        
        // 处理第一个片段
        List<String> result1 = JsonFragmentReassembler.processFragment(channelId, fragment1);
        System.out.println("处理片段1后结果: " + (result1.isEmpty() ? "等待更多数据" : "提取到 " + result1.size() + " 个完整JSON"));
        
        // 处理第二个片段
        List<String> result2 = JsonFragmentReassembler.processFragment(channelId, fragment2);
        System.out.println("处理片段2后结果: " + (result2.isEmpty() ? "等待更多数据" : "提取到 " + result2.size() + " 个完整JSON"));
        
        if (!result2.isEmpty()) {
            System.out.println("重组后的完整JSON:");
            System.out.println(result2.get(0));
            
            // 验证JSON格式
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                mapper.readTree(result2.get(0));
                System.out.println("✓ JSON格式验证通过");
            } catch (Exception e) {
                System.out.println("✗ JSON格式验证失败: " + e.getMessage());
            }
        }
    }
    
    private static void testMultipleJsonInOneMessage() {
        System.out.println("\n--- 测试2: 一个报文包含多个JSON ---");
        
        // 一个报文包含两个完整的JSON
        String message = "{\"mid\":\"msg1\",\"data\":\"value1\"}{\"mid\":\"msg2\",\"data\":\"value2\"}";
        
        String channelId = "test-multiple";
        JsonFragmentReassembler.clearBuffer(channelId);
        
        System.out.println("报文内容: " + message);
        
        List<String> results = JsonFragmentReassembler.processFragment(channelId, message);
        System.out.println("提取到 " + results.size() + " 个完整JSON:");
        
        for (int i = 0; i < results.size(); i++) {
            System.out.println("JSON " + (i + 1) + ": " + results.get(i));
            
            // 验证JSON格式
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                mapper.readTree(results.get(i));
                System.out.println("✓ JSON " + (i + 1) + " 格式验证通过");
            } catch (Exception e) {
                System.out.println("✗ JSON " + (i + 1) + " 格式验证失败: " + e.getMessage());
            }
        }
    }
    
    private static void testOneAndHalfJson() {
        System.out.println("\n--- 测试3: 一个报文包含一个半JSON ---");
        
        // 一个报文包含一个完整JSON和一个不完整的JSON
        String message = "{\"mid\":\"msg1\",\"data\":\"value1\"}{\"mid\":\"msg2\",\"data\":\"value2\"";
        
        String channelId = "test-one-and-half";
        JsonFragmentReassembler.clearBuffer(channelId);
        
        System.out.println("报文内容: " + message);
        
        List<String> results = JsonFragmentReassembler.processFragment(channelId, message);
        System.out.println("提取到 " + results.size() + " 个完整JSON:");
        
        for (int i = 0; i < results.size(); i++) {
            System.out.println("JSON " + (i + 1) + ": " + results.get(i));
            
            // 验证JSON格式
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                mapper.readTree(results.get(i));
                System.out.println("✓ JSON " + (i + 1) + " 格式验证通过");
            } catch (Exception e) {
                System.out.println("✗ JSON " + (i + 1) + " 格式验证失败: " + e.getMessage());
            }
        }
        
        // 发送第二个片段完成不完整的JSON
        String fragment2 = ",\"extra\":\"data\"}";
        System.out.println("\n发送第二个片段: " + fragment2);
        
        List<String> results2 = JsonFragmentReassembler.processFragment(channelId, fragment2);
        System.out.println("提取到 " + results2.size() + " 个完整JSON:");
        
        for (int i = 0; i < results2.size(); i++) {
            System.out.println("JSON " + (i + 1) + ": " + results2.get(i));
            
            // 验证JSON格式
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                mapper.readTree(results2.get(i));
                System.out.println("✓ JSON " + (i + 1) + " 格式验证通过");
            } catch (Exception e) {
                System.out.println("✗ JSON " + (i + 1) + " 格式验证失败: " + e.getMessage());
            }
        }
    }
} 